<?php
function eolinURL(){
	return 	"http://www.eolin.com";
}

function eolinCachePath(){
	global $blogid;
	return ROOT."/cache/$blogid/myEolin";
}

function prepareEolinCache(){
	global $blogid;
	if (!file_exists(ROOT.'/cache/'.$blogid)) {
		mkdir(ROOT.'/cache/'.$blogid);
		@chmod(ROOT.'/cache/'.$blogid,0777);
	}
	if (!is_dir(ROOT.'/cache/'.$blogid)) return false;
	if (!file_exists(ROOT.'/cache/'.$blogid.'/myEolin')) {
		mkdir(ROOT.'/cache/'.$blogid.'/myEolin');
		@chmod(ROOT.'/cache/'.$blogid.'/myEolin',0777);
	}
	if (!is_dir(ROOT.'/cache/'.$blogid.'/myEolin')) return false;
	return true;
}

function eolinCacheDelete(){
	global $blogid;
	$cachedir = eolinCachePath();
	if (is_dir($cachedir)){
		$handle = opendir($cachedir);
	    while (false !== ($file = readdir($handle))) {
			if($file != "." && $file != "..") @unlink(eolinCachePath()."/".$file);
		}
	}
}


function eolinConfigData($post){
	global $configVal;
	requireComponent( "Textcube.Function.Setting");
	$data = setting::fetchConfigVal($configVal);
	$deleteCheck = false;
	if(empty($post['userid']) || $data['userid'] != $post['userid']) {
		$deleteCheck = true;
	}
	if($data['listcnt'] != $post['listcnt']) {
		$deleteCheck = true;
	}
	if($data['listchar'] != $post['listchar']) {
		$deleteCheck = true;
	}
	if($deleteCheck) {
		eolinCacheDelete();
	}
	return true;
}

function MyEolinFile($mode, $memberfile,$data){
	requireComponent('Eolin.PHP.HTTPRequest');
	$listcnt = (empty($data['listcnt']))?10:$data['listcnt'];
	$listchar = (empty($data['listchar']))?20:$data['listchar'];
	$memberfile = eolinCachePath().$memberfile;


	if(!file_exists($memberfile)) {
		$url = eolinURL()."/myEolin/?mode=".$mode."&userid=".rawurlencode($data['userid'])."&listcnt=".$listcnt."&listchar=".$listchar."&time=".Time();
		$request = new HTTPRequest($url);
		$request->timeout = 1;
		if ($request->send()){
			$rst = $request->responseText;
			if( prepareEolinCache() && false!==($fp=@fopen($memberfile, "w"))){
				fwrite($fp,$rst);
				fclose($fp);
			}
		}else{
			@unlink($memberfile);
			$rst = "";
		}
	}else{
		$mtime = filemtime($memberfile);
		$rst = file_get_contents($memberfile);
		if((time() - $mtime) > 1800) @unlink($memberfile);
	}
	return $rst;
}

function SD_friend($parameter){
	if (isset($parameter['preview'])) {
		// preview mode
		$retval = "이올린에 등록된 나의 친구 목록을 표시합니다. 설정에서 이올린 ID 등록이 필요합니다.";
		return $retval;
	}
	return eolin_friend('');
}
function eolin_friend($target){
	global $configVal;
	requireComponent( "Textcube.Function.Setting");
	$data = setting::fetchConfigVal($configVal);
	$header = isset($data['header_friend'])?$data['header_friend']:'<h3 >내 이웃 목록</h3>';		
	$html ='';
	if(!empty($data['userid'])){
		$memberfile =  "/SD_friend_".$data['userid'].".html";
		$html = MyEolinFile('SD_friend', $memberfile,$data);
	}
	return $header.$html;
}

function CT_MyEolin($target){
	global $blogid, $blogURL, $database,$blog,$user,$pluginURL,$configVal,$mapping;
	requireComponent( "Textcube.Function.Setting");
	$data = setting::fetchConfigVal($configVal);
	$pluginName = $mapping['plugin'];
	ob_start();
?>
<script type="text/javascript" src="<?php echo $pluginURL;?>/eolin_plugin.js"></script>
	<script language="JavaScript" type="text/javascript">
	//<![CDATA[
	var pluginURL ="<?php echo $pluginURL;?>";
	function eolin_join(memberdata){
		if(document.getElementsByTagName('body')[0]!= undefined){
			viewMemberForm(memberdata);
		}else{
			window.open('<?php echo eolinURL();?>/member/blog/?'+encodeURIComponent(memberdata));
		}
		return ;
	}
	//]]>
	</script>

	<div id = "myEolinPanel" class="wrap_center">
<?php
	if(!empty($data['userid'])){
		$memberfile = "/CT_myEolin_".$data['userid'].".html";
		echo  MyEolinFile('CT_myEolin', $memberfile,$data);
	} else {

		$email = POD::queryCell("select loginid FROM {$database['prefix']}Users WHERE userid = $blogid");
		$registdata = "email=".$email."&blogurl=".rawurlencode($user['homepage'])."&blogtitle=". rawurlencode($blog['title'])."&nickname=".$user['name'];		
?>	
	<div style='width:285px;'>
		<p style='background:url("http://file.eolin.com/image/plugin/logo_center.gif") center top no-repeat; padding-top:50px; text-align:center;'>이올린을 통해 블로거들이 올리는 <br />다양한 이야기들을 만나보세요.</p>
		<p style='font-weight:bold; text-decoration:underline; margin:5px; text-align:center;'><a href="<?php echo eolinURL();?>/" target="_blank">이올린 둘러보기</a> </p>
		<p align="center">
			<input name="" type="button" value="이올린 가입하기" style='width:120px;border:2px solid #666; background-color:#fff; color:#222; font-weight:bold; padding:5px 5px 3px 5px;' onClick='return eolin_join("<?php echo $registdata;?>")' />
			<input name="" type="button" value="플러그인 설정" style='width:120px;border:2px solid #666; background-color:#fff; color:#222; font-weight:bold; padding:5px 5px 3px 5px;' onClick='window.open("<?php echo $blogURL;?>/owner/plugin/currentSetting/?Name=<?php echo $pluginName;?>","myEolin","width=500,height=500,scrollbars=1,resizable=1")' />
		</p>
		<p style='width:285px;color:#999; font-size:90%; letter-spacing:-1px; margin:5px 0;text-align:center;font-family: Verdana;'>이미 이올린에 가입하셨다면, myEolin 플러그인의 "설정"을 클릭해서 My 이올린을 활성화시킬 수 있습니다</p>
	</div>
<?php
	}
	echo "</div>";
	$target .= ob_get_contents();
	ob_end_clean();

	return $target;
}

function SD_myEolin($parameter){
	if (isset($parameter['preview'])) {
		// preview mode
		$retval = "이올린에 등록된 나의 프로필을 표시해 줍니다. 플러그인 설정에서 이올린ID등록이 필요합니다.";
		return $retval;
	}
	return eolin_myEolin('');
}
function eolin_myEolin($target){
	global $configVal;
	requireComponent( "Textcube.Function.Setting");
	$html='';
	$data = setting::fetchConfigVal($configVal);
	if(!empty($data['userid'])){
		$memberfile =  "/SD_myEolin_".$data['userid'].".html";
		$html = MyEolinFile('SD_myEolin', $memberfile,$data);
	}
	return $html;
}

function SD_mybm($parameter){
	if (isset($parameter['preview'])) {
		// preview mode
		$retval = "이올린에 북마크한 글 리스트를 출력해줍니다. 플러그인 설정에서 이올린 ID등록이 필요합니다.";
		return $retval;
	}
	return eolin_mybm('');
}

function SD_newpost($parameter){
	if (isset($parameter['preview'])) {
		// preview mode
		$retval = "이올린의 최신글 리스트를 출력해줍니다. 플러그인 설정에서 이올린 ID등록이 필요합니다.";
		return $retval;
	}
	return eolin_newpost('');
}

function eolin_newpost($target){
	global $configVal;
	$html='';	
	requireComponent( "Textcube.Function.Setting");
	$data = setting::fetchConfigVal($configVal);
	$header = isset($data['header_newpost'])?$data['header_newpost']:'<h3 >이올린 최신글</h3>';	
	if(!empty($data['userid'])){
		$memberfile =  "/SD_newpost_".$data['userid'].".html";
		$html = MyEolinFile('SD_newpost', $memberfile,$data);
	}
	return $header.$html;

}

function eolin_mybm($target){
	global $configVal;
	$html='';	
	requireComponent( "Textcube.Function.Setting");
	$data = setting::fetchConfigVal($configVal);
	$header = isset($data['header_mybm'])?$data['header_mybm']:'<h3 >내 북마크 목록</h3>';	
	if(!empty($data['userid'])){
		$memberfile =  "/SD_mybm_".$data['userid'].".html";
		$html = MyEolinFile('SD_mybm', $memberfile,$data);
	}
	return $header.$html;
}

function SD_popular($parameter){
	if (isset($parameter['preview'])) {
		// preview mode
		$retval = "이올린 인기 글 리스트를 출력해줍니다. 플러그인 설정에서 이올린 ID등록이 필요합니다.";
		return $retval;
	}
	return eolin_popular('');
}
function eolin_popular($target){
	global $configVal;
	$html='';	
	requireComponent( "Textcube.Function.Setting");
	$data = setting::fetchConfigVal($configVal);
	$header = isset($data['header_popular'])?$data['header_popular']:'<h3 >이올린 추천글</h3>';
	$memberfile =  "/eolin_popular.html";
	$html = MyEolinFile('SD_popular', $memberfile,$data);
	return $header.$html;
}

function myEolin_ConfigOut_ko($plugin) {

	$manifest = '';
	
	$manifest .= '<?xml version="1.0" encoding="utf-8"?>'.CRLF;
	$manifest .= '<config dataValHandler="">'.CRLF;
	$manifest .= '	<window width="500" height="500" />'.CRLF;
	$manifest .= '		<fieldset legend="이올린 로그인 정보">'.CRLF;
	$manifest .= '				<field title="아 이 디 : " name="userid" type="text" value=""></field>'.CRLF;
	$manifest .= '		</fieldset>'.CRLF;
	$manifest .= '		<fieldset legend="목록 형식 설정">'.CRLF;
	$manifest .= '		<field title="출력갯수 " name="listcnt" type="select">'.CRLF;
	$manifest .= '			<op value="3" >3개</op>'.CRLF;
	$manifest .= '			<op value="5" checked="checked">5개</op>'.CRLF;
	$manifest .= '			<op value="10">10개</op>'.CRLF;
	$manifest .= '			<op value="20">15개</op>'.CRLF;
	$manifest .= '			<op value="20">20개</op>'.CRLF;
	$manifest .= '		</field>'.CRLF;

	$manifest .= '		<field title="글자수 " name="listchar" type="select">'.CRLF;
	for($i=1;$i <41; $i++ ){
		if($i == 20) 	$manifest .= '			<op value="'.$i.'" checked="checked">'.$i.'자</op>'.CRLF;
		else 	$manifest .= '			<op value="'.$i.'" >'.$i.'자</op>'.CRLF;
	}
	$manifest .= '			<caption>'.CRLF;
	$manifest .= '				<![CDATA['.CRLF;
	$manifest .= '				사이드바에 표시되는 목록의 갯수와 글자수를  설정합니다.'.CRLF;
	$manifest .= '				]]>'.CRLF;
	$manifest .= '			</caption>'.CRLF;
	$manifest .= '		</field>'.CRLF;
	$manifest .= '		</fieldset>'.CRLF;

	$manifest .= '		<fieldset legend="이올린에 북마크하기, 추천하기">'.CRLF;
	$manifest .= '		<field title="이 기능을" name="bookmark" type="radio">'.CRLF;
	$manifest .= '			<op value="T" checked="checked">사용합니다</op>'.CRLF;
	$manifest .= '			<op value="F" >사용하지 않습니다</op>'.CRLF;
	$manifest .= '			<caption>'.CRLF;
	$manifest .= '				<![CDATA['.CRLF;
	$manifest .= '				블로그 포스트하단에 이올린에 북마크하기, 추천하기 버튼이 추가됩니다.<br />'.CRLF;
	$manifest .= '				플러그인의 CSS 수정은 plugins/'.$plugin.'/ 폴더의 myEolin.css를 수정하시면 됩니다.'.CRLF;
	$manifest .= '				<br />MyEolin 플러그인에 대한 자세한 설명은 <a href="http://blog.eolin.com/category/plugin" target="_blank">http://blog.eolin.com</a> 을 방문하세요.<br />'.CRLF;
	$manifest .= '				]]>'.CRLF;
	$manifest .= '			</caption>'.CRLF;
	$manifest .= '		</field>'.CRLF;
	$manifest .= '		</fieldset>'.CRLF;
	$manifest .= '		<fieldset legend="사이드 바 Title 설정">'.CRLF;
	$manifest .= '			<field title="내 이웃목록" name="header_friend" type="textarea" cols="50" rows="2" >';
	$manifest .= htmlspecialchars('<h3>내 이웃 목록</h3>');
	$manifest .= '			</field >'.CRLF;
	$manifest .= '			<field title="내 북마크 목록" name="header_mybm" type="textarea" cols="50" rows="2" >';
	$manifest .= htmlspecialchars('<h3>내 북마크 목록</h3>');
	$manifest .= '			</field >'.CRLF;
	$manifest .= '			<field title="이올린 추천글" name="header_popular" type="textarea" cols="50" rows="2" >';
	$manifest .= htmlspecialchars('<h3><a href="'.eolinURL().'"  target="_blank">이올린 추천글</a></h3>');
	$manifest .= '			</field >'.CRLF;
	$manifest .= '			<field title="이올린 최신글" name="header_newpost" type="textarea" cols="50" rows="2" >';
	$manifest .= htmlspecialchars('<h3><a href="'.eolinURL().'/post" target="_blank">이올린 최신글</a></h3>');
	$manifest .= '			</field >'.CRLF;
	$manifest .= '		</fieldset>'.CRLF;
	$manifest .= '</config>';
	
	return $manifest;
}

function getSaveEolin_php($target,$mother){
	global $blogid,$database;
	include_once('getSaveEolin.php');
	$entry_id = isset($_GET['id'])?$_GET['id']:null;
	if(!Validator::id($entry_id)){echo 0;flush();return;}
	$entry = POD::queryRow("SELECT title, content FROM {$database['prefix']}Entries WHERE blogid = $blogid AND id = $entry_id AND draft = 0 AND visibility > 0");	
	if(!is_null($entry)){
		$title = $entry['title'];
		$desc =  stripHTML(removeAllTags($entry['content']));		
		$desc = UTF8::lessen($desc, 128); 
		$entryTags=getTags($entry_id);
		if(sizeof($entryTags)>0){
			$tags=array();
			foreach($entryTags as $entryTag)
				array_push($tags,encodeURL($entryTag['name']));
			$tag = implode(",",$tags);
		}				
	
		$data = "title=".rawurlencode($title)."&tag=".rawurlencode($tag)."&description=".rawurlencode($desc);
	}else{
		$data = "0";
	}		
	echo $data;
	flush();
	return;
}

function Bookmark_JSEolin($target){
	global $blogURL, $pluginURL,$blogid;

	$callurl = $blogURL."/plugin/getSaveEolin/?id=";
ob_start();
?>
<script type="text/javascript" src="<?php echo $pluginURL;?>/eolin_plugin.js"></script>
<link rel="stylesheet" type="text/css" href="<?php echo $pluginURL;?>/myEolin.css" />
<script language="JavaScript" type="text/javascript">
//<![CDATA[
	var apiEolinURL = new Array();
	var pluginURL = "<?php echo $pluginURL;?>";
	function bookMarkEolin(id,permalink,gubun) {
		var getEntryURL = "<?php echo $callurl;?>"+id;
		var bookmarkdata  = requestHttpText(getEntryURL);

		if(bookmarkdata == "0"){
			alert("비공개글입니다");
		}else{
			if(document.getElementsByTagName('body')[0]!= undefined){
				viewBookmarkForm(bookmarkdata+"&url="+encodeURIComponent(permalink),gubun);
			}else{
				if (document.getSelection){
					description=document.getSelection();
				}else if(document.selection && document.selection.createRange) {
					var range = document.selection.createRange(); 
					var description = range.text;
				}else{
					var description='';
				}
			
				if(gubun =="bookmark"){	
				window.open('<?php echo eolinURL();?>/bookmark/post.php?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&description='+encodeURIComponent(description));
				}else{
				window.open('<?php echo eolinURL();?>/bookmark/vote.php?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&description='+encodeURIComponent(description));
				}
			}
		}
	}
 //]]>
</script>
<?php
	$target .= ob_get_contents();
	ob_end_clean();
	return $target;
}

function eolin_print($target){
	global $configVal;
	ob_start();
?>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
	if(apiEolinURL.length > 0){
		for(i=0;i<apiEolinURL.length;i++){
			loadScript(apiEolinURL[i]);
		}
	}
//]]>
</script>
<?
	$target .= ob_get_contents();
	ob_end_clean();
	return $target;
}
function Bookmark_iframe($target){

	ob_start();
?>
 	<div id='bookmark_handle' style='display:none;position:absolute; top:10px; left:105px; height:570px; width:550px; z-index:9999999; background:#FFF url(http://file.eolin.com/image/plugin_pop.gif)  left top repeat-x; border:1px solid #666;'>
	<div id='mini_bookmark_handle' style='background:url(http://file.eolin.com/image/plugin_logo.gif) 3px 3px no-repeat; text-align:left; z-index:9999998; width:432px; height:48px; cursor:move; float:left'></div>
	<div id='mini_close' style='margin:11px 10px 0 0; float:right'><a href='javascript:closePopup()' title='close'><img src='http://file.eolin.com/image/icon_close.gif' border='0'  alt='close' /></a></div>
	<div style='clear:both;'><iframe id="eolin_iframe" src='about:blank' width='550' height='500' scrolling='no' frameborder='no'></iframe></div>
    </div>
<?php
	$target .= ob_get_contents();
	ob_end_clean();
	return $target;
}

function Bm_addPost($target){
	global $database;
	global $pluginURL, $blogid,$defaultURL;	
	$syncURL = $defaultURL."/sync/".$target;
	$data = POD::queryRow("select * from {$database['prefix']}EolinCategory where blogid=$blogid and entryId=0");
	$entryId = $target;
	if(!is_null($data)){
		$eolinCategoryId = (!empty($data['eolinCategoryId']))?$data['eolinCategoryId']:"NULL";
		$eolinThemeId = (!empty($data['eolinThemeId']))?$data['eolinThemeId']:"NULL";
		POD::query("update {$database['prefix']}EolinCategory set entryId=$entryId where blogid=$blogid and entryId=0");
			$syncURL = $defaultURL."/sync/".$entryId;
			if($entryId > 0 ){
				$syncURL = $defaultURL."/sync/".$entryId;
				$eolinsyncURL = $defaultURL."/plugin/getEolinCategory/?syncURL=".rawurlencode($syncURL);
				sendEolinPing($eolinsyncURL);
			}
	}
	return $target;
}

function Bm_myEolin($target, $mother){
	global $hostURL, $blogURL, $pluginURL,$blogid,$permalink,$entry,$configVal;
	requireComponent( "Textcube.Function.Setting");
	$data = setting::fetchConfigVal($configVal);
	$getMyvote = 1;
	
	if(stristr($_SERVER['SCRIPT_FILENAME'],"blog/sync/item.php")) return $target;
	if(stristr($_SERVER['SCRIPT_FILENAME'],"blog/rss/index.php")) return $target;	
	if(stristr($_SERVER['SCRIPT_FILENAME'],"blog/m/item.php")) return $target;	
	if(!isset($entry['id'])||!Validator::id($entry['id'])) return $target;
	if(!empty($data) && $data['bookmark'] != "T" ) return $target;

	if($entry['visibility'] >= "2" ){
		$target .= '<div class="EolinBmVote">';
		$img = $pluginURL."/images/bookmarkToEolin.gif";
		$img2 = $pluginURL."/images/voteToEolin.gif";
		$permalink = $hostURL. (isset($permalink)?$permalink:'');
		
		$ownership = false;
		if(true==function_exists('doesHaveOwnership')){
			$ownership = doesHaveOwnership();
		}

		if($ownership != false){
			$url = "http://api.eolin.com/myEolin/getEntryInfo.js?permalink=".rawurlencode($permalink)."&id=".$entry['id']."&log=0";
		}else{
			$url = "http://api.eolin.com/myEolin/getEntryInfo.js?permalink=".rawurlencode($permalink)."&id=".$entry['id']."&log=1";
		}		
		
		
		$target .=  '<span class="EolinBookmarkBtn"><a href="#void"  title="이올린에 북마크하기" onclick="bookMarkEolin('.$entry['id'].', \''.$permalink.'\', \'bookmark\');return false;"><img src="'.$img.'" border="0" alt="이올린에 북마크하기" style="vertical-align:middle;" /></a></span>';
		if($entry['visibility'] == "3" && $getMyvote == 1){
			$target .=  '(<span id="bookmarkEolin'.$entry['id'].'" class="EolinBookmarkCount">0</span>) <span class="EolinVoteBtn"><a href="#void"  title="이올린에 추천하기" onclick="bookMarkEolin('.$entry['id'].',\''.$permalink.'\', \'vote\');return false;"><img src="'.$img2.'" border="0" alt="이올린에 추천하기" style="vertical-align:middle;" /></a></span>(<span id="voteEolin'.$entry['id'].'" class="EolinVoteCount">0</span>)';
			$target .= "<script language=\"JavaScript\" type=\"text/javascript\">//<![CDATA[	
								apiEolinURL.push(\"".$url."\");  
								//]]></script>";										
			//$target .= "<script src='".$url."' type='text/javascript' ></script>";
		}
		$target .= "</div>";
	}
	return $target;
}
function sendEolinPing($url) {
	requireComponent('Eolin.PHP.Core');
	requireComponent('Eolin.PHP.XMLRPC');
	$rpc = new XMLRPC();
	$rpc->url = TEXTCUBE_SYNC_URL;
	$summary = array(
		'url' => $url
	);
	$rpc->async = true;
	$rpc->call('sync.eolinCategory', $summary);
}
function getEolinCategory_php($target, $mother){
	global $blogid,$database,$defaultURL;

	$syncURL = isset($_GET['syncURL'])?$_GET['syncURL']:null;
	$rst = array();
	$rst['error'] = "1";
	if(!is_null($syncURL)){

		 preg_match("/(\d+)$/",$syncURL, $data);
		$id= $data[1];
		$category = POD::queryRow("select eolinCategoryId, eolinThemeId from {$database['prefix']}EolinCategory where blogid=$blogid and entryId=$id");
		if(!empty($category)){
			$rst['syncURL'] = $syncURL;
			$rst['eolinCategoryId'] = $category['eolinCategoryId'];
			$rst['eolinThemeId'] = $category['eolinThemeId'];
			$rst['error'] = "0";
			printRespond($rst, false);
			return;
		}		
	}
				printRespond($rst, false);
	return;
}

function getMyEolin_php($target, $mother){
	global $pluginURL, $service,$blogid,$entry,$defaultURL,$database,$blogURL;
	global $configVal;
	requireComponent( "Textcube.Function.Setting");
	$data = setting::fetchConfigVal($configVal);
	include 'getMyEolin.php';
}
function saveEolinCategory_php($target, $mother){
	global $blogid,$database,$defaultURL;
	requireComponent("Eolin.PHP.HTTPRequest");
	$id = isset($_GET['id'])?$_GET['id']:null;
	$eolinCategoryId	= ($_GET['eolinCategoryId']=="")?"NULL":$_GET['eolinCategoryId'];
	$eolinThemeId	= ($_GET['eolinThemeId']=="")?"NULL":$_GET['eolinThemeId'];
	if(!is_null($id) && Validator::id($id, 0)){
		if(POD::execute("replace into {$database['prefix']}EolinCategory(blogid,entryId, eolinCategoryId, eolinThemeId) values($blogid, $id, $eolinCategoryId, $eolinThemeId)")){
			$category = POD::queryRow("select * from {$database['prefix']}EolinCategory where blogid=$blogid and entryId=$id");
			if(is_null($category)){flush();return;}
			$syncURL = $defaultURL."/sync/".$id;
			if($id > 0 ){
				$syncURL = $defaultURL."/sync/".$id;
				$eolinsyncURL = $defaultURL."/plugin/getEolinCategory/?syncURL=".rawurlencode($syncURL);
				sendEolinPing($eolinsyncURL);
			}
		}
	}
	flush();
	return;
}
function EolinMybm($target, $mother){
	global $pluginURL, $service,$blogid,$entry,$defaultURL,$database,$blogURL;
	global $configVal;
	requireComponent( "Textcube.Function.Setting");
	$data = setting::fetchConfigVal($configVal);
	$memberfile = "/eolinCategory.html";
	$eolinCategory = MyEolinFile('eolinCategory', $memberfile,$data);
	$eolinCategoryId ="";
	$eolinThemeId ="";
	if($entry['id'] > 0){
		$_EolinCategory = POD::queryRow("select * from {$database['prefix']}EolinCategory where blogid=$blogid and entryId={$entry['id']}");
		if(!is_null($_EolinCategory )){
			$eolinCategoryId = !empty($_EolinCategory ['eolinCategoryId'])?$_EolinCategory ['eolinCategoryId']:"NULL";
			$eolinThemeId = !empty($_EolinCategory ['eolinThemeId'])?$_EolinCategory ['eolinThemeId']:"NULL";
		}
	}else{
		POD::execute("delete from {$database['prefix']}EolinCategory where blogid=$blogid and entryId=0");
	}

	$openURL =  $blogURL . "/plugin/getMyEolin/";
	$buttonURL = $pluginURL . "/images/btn_usebookmark.gif";

	$thisTarget ='';
	ob_start();
?>
	
	<div id="layout_category">
		<div>
			<div class="eolin_title">
				<strong>이올린 분류</strong>:
				<span>
					<a href="#void" onClick="toggleLayer('eolincategory'); return false;" >
						이올린 카테고리·테마를 선택하여 글을 보냅니다
					</a> 
				</span>
			</div>
			<div class="use_bookmark">
				<a href="#void" onClick="openMyEolin();return false;">
					<img src="<?php echo $buttonURL;?>" alt="내 북마크 넣기" border='0' />
				</a>
			</div>	
		</div>
		<div style="clear:both;"></div>
		<div id="eolincategory" style="display:none;"><?php echo $eolinCategory;?></div>	
	</div>
	<script type='text/javascript'>
	//<![CDATA[
	function addBookMarkInfo(str) {
		var isWYSIWYG = false;
		try {
			if(editor.editMode =="WYSIWYG")
			isWYSIWYG = true;
		} catch(e) { }
		TTCommand("Raw", (str), " ");
		editor.trimContent();
	}
	function openMyEolin() {
		nwin =window.open('<?php echo $openURL;?>', 'myeolin','top=100,left=100,width=600,height=650,scrollbars=1,resizable=1'); 
		nwin.focus();
	}
	function saveEolinCategory(){
		var id = "<?php echo $entry['id'];?>";
		var blogid ="<?php echo $blogid;?>";
		var oForm = document.forms[0];
		var eolinCategoryId="";
		var eolinThemeId="";
	
		for(i=0;i < oForm.categoryId.length;i++){
			if(oForm.categoryId[i].checked){
				eolinCategoryId = oForm.categoryId[i].value;
				break;
			}
		}
		for(i=0;i < oForm.themeId.length;i++){
			if(oForm.themeId[i].checked){
				eolinThemeId = oForm.themeId[i].value;
				break;
			}
		}
	
			var url = '<?php echo $blogURL;?>/plugin/saveEolinCategory/?id='+id+'&eolinCategoryId='+eolinCategoryId+'&eolinThemeId='+eolinThemeId;
			var request = new HTTPRequest(url);
			request.persistent = false;
			request.send();
	}

<?php 
	if($eolinCategoryId > 0 )	{
?>
	document.forms[0].categoryId[<?php echo ($eolinCategoryId -1);?>].checked = true;
<?	 }
	if($eolinThemeId > 0 )	{?>
	document.forms[0].themeId[<?php echo ($eolinThemeId -1);?>].checked = true;
<?php 
	}
?>
	//]]>
	</script>
<?php
	$thisTarget .= ob_get_contents();
	ob_end_clean();
	$target .= $thisTarget;
	return $target;
}

if( !function_exists('printRespond')){
function printRespond($result,$useCDATA=true){
	header('Content-Type: text/xml; charset=utf-8');
	$xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
	$xml.="<response>\n";
	$xml.=printRespondValue($result,$useCDATA);
	$xml.="</response>\n";
	die($xml);
}
}

if( !function_exists('printRespondValue')){
function printRespondValue($array,$useCDATA=true){
	$xml='';
	if(is_array($array)){
		foreach($array as $key=>$value){
			if(is_null($value))
				continue;
			elseif(is_array($value)){
				if(is_numeric($key))
					$xml.=printRespondValue($value,$useCDATA)."\n";
				else
					$xml.="<$key>".printRespondValue($value,$useCDATA)."</$key>\n";
			}else{
				if($useCDATA)
					$xml.="<$key><![CDATA[".escapeCData($value)."]]></$key>\n";
				else
					$xml.="<$key>".htmlspecialchars($value)."</$key>\n";
			}
		}
	}
	return $xml;
}
}
?>
